// components/authorizaLogin/authorizaLogin.js
const app = getApp()
const {
  unionAsset
} = require('../../api/api');
const WXAPI = require('../../api/api');
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    chose: {
      type: Boolean,
      value: false,
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    loginShow: false,
    isShow: false, //同意服务协议弹窗
    isChoice: false, //是否点击已同意
    isPrivacy: true, //是否已经授权同意隐私协议
  },

  attached() {
    this.getCode()
    this.setNavSize()
    // this.getWxPrivacy()
  },
  /**
   * 组件的方法列表
   */
  methods: {
    getWxPrivacy() {
      if(wx.getPrivacySetting){
        wx.getPrivacySetting({
          success: res => {
            console.log(res) // 返回结果为: res = { needAuthorization: true/false, privacyContractName: '《xxx隐私保护指引》' }
            if (res.needAuthorization) {
              // 需要弹出隐私协议
              console.log('隐私协议未授权')
              this.setData({
                isPrivacy: false,
                isChoice: false,
              })
            } else {
              // 用户已经同意过隐私协议，所以不需要再弹出隐私协议，也能调用隐私接口
              console.log('隐私协议已授权')
              this.setData({
                isPrivacy: true,
              })
            }
          },
        })
      }
    },
    //关闭授权登录
    loginHide() {
      this.setData({
        loginShow: false,
      })
    },
    //显示授权登录
    loginShow() {
      this.getWxPrivacy()
      this.setData({
        loginShow: true,
      })
    },
    handleChose() {
      console.log(1111111111111)
      this.loginHide()
      this.triggerEvent('handleChose')
    },
    //点击我已阅读
    loginOut() {
      if (this.data.isPrivacy&&!this.data.isChoice) {
        wx.showToast({
          title: '请点击我已阅读并同意',
          icon: 'none'
        })
      }else if (!this.data.isPrivacy) {
        this.setData({
          isShow: true
        })
      }
    },
    //不同意
    closeShow() {
      this.setData({
        isShow: false
      })
    },
    // 点击我已同意
    choiceClick() {
      if (!this.data.isPrivacy) { //未授权情况
        this.setData({
          isShow: true
        })
      }else{
        this.data.isChoice = !this.data.isChoice
        this.setData({
          isChoice: this.data.isChoice
        })
      }
    },
    // 用户同意隐私协议事件回调
    handleAgreePrivacyAuthorization() {
      this.setData({
        isShow: false,
        isPrivacy: true,
        isChoice: true,
      })
    },
    getCode() {
      // 登录 
      let that = this;
      wx.login({
        success: function (res) {
          if (res.code) {
            that.data.code = res.code
          } else {
            console.log('获取code失败', res.errMsg)
          }
        }
      })
    },
    //授权手机号码
    getPhoneNumber: function (e) {
      this.getCode()
      let that = this;
      console.log(e.detail)
      console.log(e.detail.errMsg)
      console.log(e.detail.iv)
      console.log(e.detail.encryptedData)
      console.log(e.detail.errMsg)
      if (e.detail.errMsg == 'getPhoneNumber:ok') {
        console.log("点击事件", that.data.code)
        let data = {
          appid: app.globalData.appid,
          code: that.data.code,
          encryptedData: e.detail.encryptedData,
          iv: e.detail.iv,
          source: app.globalData.source,
        }
        WXAPI.newgetPhone('', data, (res) => {
          let data = res.result
          if (res.code == 200) {
            this.data.phone = data.phone
            this.data.sessionKey = data.sessionKey
            this.data.encryption = data.encryption
            wx.setStorageSync('phone', data.phone)
            if (data.token == '') {
              // that.setData({
              //   isShow: true
              // })
              that.getLogin()
            } else {
              wx.setStorageSync('token', data.token)
              that.setData({
                loginShow: false
              })
              that.triggerEvent("loginSuccess")
            }
          } else {
            wx.showToast({
              title: data.message,
              icon: 'none',
              duration: 2000
            })

          }
        })
      } else if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
        //ios拒绝授权 
        that.triggerEvent("notokenFuntion");
      } else if (e.detail.errMsg == 'getPhoneNumber:fail:user deny') {
        //安卓拒绝授权    
      }
    },
    //微信登录
    getLogin() {
      var that = this
      var data = {
        appid: app.globalData.appid,
        sessionKey: this.data.sessionKey,
        phone: this.data.phone,
        encryption: this.data.encryption,
        source: app.globalData.source, //用户来源
      }
      WXAPI.newuserLogin(data, (res) => {
        console.log("用户登录成功", res)
        let data = res.result
        if (res.code == 200) {
          wx.setStorageSync('token', data.token)
          that.setData({
            loginShow: false,
          })
          that.triggerEvent("loginSuccess")
        } else {
          wx.showToast({
            title: res.message,
            icon: 'none',
            duration: 2000
          })
        }
      })
    },
    //授权用户信息(已弃用)
    getUserProfile(e) {
      let that = this;
      let sessionKey = that.data.sessionKey;
      wx.getUserProfile({
        desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
        success: (res) => {
          console.log("res", res)
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称
          var data = {
            appid: app.globalData.appid,
            sessionKey: sessionKey,
            encryptedData: res.encryptedData,
            iv: res.iv,
            signature: res.signature,
            rawData: res.rawData,
            phone: this.data.phone,
            encryption: that.data.encryption,
            source: app.globalData.source, //用户来源
          }
          WXAPI.newuserLogin(data, (res) => {
            console.log("用户登录成功", res)
            let data = res.result
            if (res.code == 200) {
              wx.setStorageSync('token', data.token)
              that.setData({
                loginShow: false,
              })
              that.triggerEvent("loginSuccess")
            } else {
              wx.showToast({
                title: res.message,
                icon: 'none',
                duration: 2000
              })
            }

          })
        },
        fail(res) {
          console.log("获取用户信息失败", res)
        }
      })

    },
    // 通过获取系统信息计算导航栏高度
    setNavSize: function () {
      this.setData({
        statusHeight: app.gettop.height,
        status: app.gettop.status,
        navHeight: app.gettop.navHeight,
        mobbleBottom: app.mobile.mobbleBottom
      })
    },
    getPrivacy() { //隐私政策
      wx.navigateTo({
        url: '/userPageC/pages/file/privacyPolicy/privacyPolicy',
      })
    },
    //服务协议
    getService() {
      wx.navigateTo({
        url: '/userPageC/pages/file/service/service',
      })
    },
  }
})